<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改密码 - 名片维护系统</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css">
    <style>
        /* Reset and Base Styles (Consistent with other admin pages) */
        :root {
            --primary-color: #3498db;
            --primary-hover-color: #2980b9;
            --sidebar-bg: #2c3e50;
            --sidebar-text-color: #ecf0f1;
            --sidebar-active-link-bg: #34495e;
            --sidebar-active-link-border: var(--primary-color);
            --content-bg: #f4f6f9;
            --text-color: #333;
            --text-muted-color: #777;
            --card-bg: #ffffff;
            --card-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            --border-color: #e0e0e0;
            --success-color: #2ecc71;
            --error-color: #e74c3c;
            --input-border-color: #ced4da;
            --input-focus-border-color: var(--primary-color);
            --button-primary-bg: var(--primary-color);
            --button-primary-hover-bg: var(--primary-hover-color);
            --font-family: 'Roboto', 'Helvetica Neue', Arial, sans-serif;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: var(--font-family);
            background-color: var(--content-bg);
            color: var(--text-color);
            line-height: 1.6;
            display: flex;
        }

        a {
            text-decoration: none;
            color: var(--primary-color);
        }

        a:hover {
            color: var(--primary-hover-color);
        }

        /* Admin Container */
        .admin-container {
            display: flex;
            width: 100%;
            min-height: 100vh;
        }

        /* Sidebar */
        .sidebar {
            width: 260px;
            background-color: var(--sidebar-bg);
            color: var(--sidebar-text-color);
            padding: 20px 0;
            display: flex;
            flex-direction: column;
            flex-shrink: 0;
        }

        .sidebar-title {
            font-size: 1.5em;
            font-weight: 600;
            text-align: center;
            padding: 0 20px 20px 20px;
            margin-bottom: 20px;
            border-bottom: 1px solid rgba(236, 240, 241, 0.1);
            color: var(--sidebar-text-color);
        }

        .menu-list {
            list-style-type: none;
        }

        .menu-item .menu-link {
            display: block;
            padding: 15px 25px;
            color: var(--sidebar-text-color);
            font-size: 1em;
            transition: background-color 0.3s ease, color 0.3s ease, border-left 0.3s ease;
            border-left: 3px solid transparent;
            text-decoration: none; /* 添加这行去除下划线 */
        }

        .menu-item .menu-link .fa {
            margin-right: 10px;
            width: 20px; /* Align icons */
            text-align: center;
        }

        .menu-item .menu-link:hover {
            background-color: var(--sidebar-active-link-bg);
            border-left-color: var(--primary-hover-color);
            color: #fff;
        }

        .menu-item .menu-link.active {
            background-color: var(--sidebar-active-link-bg);
            border-left-color: var(--sidebar-active-link-border);
            font-weight: 600;
            color: #fff;
        }

        /* Main Content */
        .main-content {
            flex-grow: 1;
            background-color: var(--content-bg);
            padding: 0;
            display: flex;
            flex-direction: column;
        }

        /* Header */
        .header {
            background-color: var(--card-bg);
            padding: 15px 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
            border-bottom: 1px solid var(--border-color);
        }

        .header-title {
            font-size: 1.8em;
            font-weight: 600;
            color: var(--text-color);
        }

        .logout-button {
            background-color: var(--button-primary-bg);
            color: white;
            padding: 8px 15px;
            border-radius: 5px;
            font-size: 0.9em;
            transition: background-color 0.3s ease;
            border: none;
            cursor: pointer;
        }

        .logout-button:hover {
            background-color: var(--button-primary-hover-bg);
            color: white;
        }

        .logout-button .fa {
            margin-right: 5px;
        }

        /* Content Area */
        .content-area {
            padding: 30px;
            flex-grow: 1;
        }

        /* Form Container and Elements */
        .form-container {
            background-color: var(--card-bg);
            padding: 30px;
            border-radius: 8px;
            box-shadow: var(--card-shadow);
            max-width: 600px; /* Or your preferred max-width */
            margin: 0 auto; /* Center the form if it's narrower than content-area */
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            font-weight: 600;
            margin-bottom: 8px;
            color: var(--text-color);
            font-size: 0.95em;
        }

        .form-input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid var(--input-border-color);
            border-radius: 5px;
            font-size: 1em;
            transition: border-color 0.3s ease, box-shadow 0.3s ease;
        }

        .form-input:focus {
            outline: none;
            border-color: var(--input-focus-border-color);
            box-shadow: 0 0 0 0.2rem rgba(52, 152, 219, 0.25);
        }

        .form-button {
            background-color: var(--button-primary-bg);
            color: white;
            padding: 12px 20px;
            border: none;
            border-radius: 5px;
            font-size: 1em;
            font-weight: 600;
            cursor: pointer;
            transition: background-color 0.3s ease;
            width: 100%; /* Make button full width */
        }

        .form-button:hover {
            background-color: var(--button-primary-hover-bg);
        }
        .form-button .fa {
            margin-right: 8px;
        }

        /* Message Styles (Consistent with other admin pages) */
        .message {
            padding: 15px 20px;
            margin-bottom: 20px;
            border-radius: 5px;
            font-size: 0.95em;
            display: flex; /* Use flex for icon alignment */
            align-items: center;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        .message .fa {
            margin-right: 10px;
            font-size: 1.2em;
        }
        .message.success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .message.error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

    </style>
</head>
<body>
    <div class="admin-container">
        <!-- 侧边栏 -->
        <div class="sidebar">
            <h2 class="sidebar-title">名片维护系统</h2>
            <ul class="menu-list">
                <li class="menu-item">
                    <a class="menu-link" href="/admin/index"><i class="fa fa-home"></i>首页</a>
                </li>
                <li class="menu-item">
                    <a class="menu-link" href="/admin/user-management"><i class="fa fa-users"></i>用户管理</a>
                </li>
                <li class="menu-item">
                    <a class="menu-link" href="/admin/usergroup"><i class="fa fa-user-circle"></i>用户组管理</a>
                </li>
                <li class="menu-item">
                    <a class="menu-link active" href="/admin/change-password"><i class="fa fa-key"></i>修改密码</a>
                </li>
            </ul>
        </div>

        <!-- 主要内容 -->
        <div class="main-content">
            <div class="header">
                <h2 class="header-title">修改密码</h2>
                <a class="logout-button" href="/admin/logout"><i class="fa fa-sign-out"></i>退出登录</a>
            </div>

            <div class="content-area">
                <div class="form-container">
                    <div class="message error" id="error-message" style="display: none;">
                        <i class="fa fa-times-circle"></i> <span></span>
                    </div>
                    <div class="message success" id="success-message" style="display: none;">
                        <i class="fa fa-check-circle"></i> <span></span>
                    </div>

                    <form id="change-password-form">
                        <div class="form-group">
                            <label class="form-label" for="old-password"><i class="fa fa-unlock-alt"></i> 旧密码</label>
                            <input class="form-input" type="password" id="old-password" name="oldPassword" required>
                        </div>

                        <div class="form-group">
                            <label class="form-label" for="new-password"><i class="fa fa-lock"></i> 新密码</label>
                            <input class="form-input" type="password" id="new-password" name="newPassword" required>
                        </div>

                        <div class="form-group">
                            <label class="form-label" for="confirm-password"><i class="fa fa-lock"></i> 确认新密码</label>
                            <input class="form-input" type="password" id="confirm-password" name="confirmPassword" required>
                        </div>

                        <button class="form-button" type="submit"><i class="fa fa-save"></i> 修改密码</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script>
        const changePasswordForm = document.getElementById('change-password-form');
        const oldPasswordInput = document.getElementById('old-password');
        const newPasswordInput = document.getElementById('new-password');
        const confirmPasswordInput = document.getElementById('confirm-password');

        const errorMessageElement = document.getElementById('error-message');
        const successMessageElement = document.getElementById('success-message');
        const errorMessageSpan = errorMessageElement.querySelector('span');
        const successMessageSpan = successMessageElement.querySelector('span');

        changePasswordForm.addEventListener('submit', function(e) {
            e.preventDefault();

            const oldPassword = oldPasswordInput.value;
            const newPassword = newPasswordInput.value;
            const confirmPassword = confirmPasswordInput.value;

            // Clear previous messages
            hideMessages();

            // Basic Form Validation
            if (!oldPassword || !newPassword || !confirmPassword) {
                showError('所有密码字段均为必填项。');
                return;
            }

            if (newPassword.length < 6) { // Example: Minimum password length
                showError('新密码长度至少为6个字符。');
                return;
            }

            if (newPassword !== confirmPassword) {
                showError('新密码与确认新密码不匹配。');
                return;
            }

            // Prepare form data
            const formData = new URLSearchParams();
            formData.append('oldPassword', oldPassword);
            formData.append('newPassword', newPassword);
            formData.append('confirmPassword', confirmPassword); // Though confirmPassword is often validated client-side, server might also check

            // Send request
            fetch('/admin/change-password', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                body: formData.toString()
            })
            .then(response => {
                if (!response.ok) { // Check for non-2xx responses
                    return response.json().then(errData => {
                        throw new Error(errData.message || `HTTP error! status: ${response.status}`);
                    });
                }
                return response.json();
            })
            .then(data => {
                if (data.success) {
                    showSuccess(data.message || '密码修改成功！3秒后将跳转到登录页面。');
                    changePasswordForm.reset();
                    setTimeout(function() {
                        window.location.href = '/admin/login';
                    }, 3000);
                } else {
                    showError(data.message || '密码修改失败，请重试。');
                }
            })
            .catch(error => {
                showError('发生通讯错误或服务器内部错误，请稍后再试。');
                console.error('Change Password Error:', error);
            });
        });

        function hideMessages() {
            if (errorMessageElement) errorMessageElement.style.display = 'none';
            if (successMessageElement) successMessageElement.style.display = 'none';
            if (errorMessageSpan) errorMessageSpan.textContent = '';
            if (successMessageSpan) successMessageSpan.textContent = '';
        }

        function showError(message) {
            hideMessages();
            if (errorMessageElement && errorMessageSpan) {
                errorMessageSpan.textContent = message;
                errorMessageElement.style.display = 'flex';
            }
             setTimeout(() => {
                if (errorMessageElement) errorMessageElement.style.display = 'none';
            }, 5000); // Error messages stay a bit longer
        }

        function showSuccess(message) {
            hideMessages();
            if (successMessageElement && successMessageSpan) {
                successMessageSpan.textContent = message;
                successMessageElement.style.display = 'flex';
            }
            setTimeout(() => {
                if (successMessageElement) successMessageElement.style.display = 'none';
            }, 3000);
        }
    </script>
</body>
</html>
